package CutLoad.cookies
{
	import flash.events.*;
	import flash.net.*;
	
	/**
	 * URLLoader简化
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	public class CURLLoader
	{
		private var funLoadCompt:Function;
		private var funLoadError:Function;
		private var url:URLRequest;
		private var loader:URLLoader;
		/**
		 * 其它附加信息
		 */		
		private var _objOtherInfo:Object=new Object();
		/**
		 * 初始化 
		 * @param url 地址
		 * @param funLoadCompt 完成时做的事 格式：function(data:String,me:CURLLoader);
		 * @param updata 上传的数据，如果为空，表示不上传，注：这里采用的是post
		 * @param funLoadError 错误时做的事   function()
		 * 
		 */		
		public function CURLLoader(url:String,funLoadCompt:Function=null,updata:String="",funLoadError:Function=null)
		{
			
			this.url=new URLRequest(url);
			if(updata!=""){
				this.url.method=URLRequestMethod.POST;
				var vars:URLVariables=new URLVariables();
				vars.data=updata;
				this.url.data=vars;
			}
			if(funLoadCompt==null){
				funLoadCompt=function(data:String,me:CURLLoader):void{
				}
			}
			this.funLoadCompt=funLoadCompt;
			if(funLoadError==null) funLoadError=function():void{};
			this.funLoadError=funLoadError;
			loader=new URLLoader();
			addEventListener(loader);
		}
		/**
		 * 设置额外附加信息,注：使用这个方法会清空之前保留的信息，如果要保留之前的一些属性，请使用这个方法：appendOtherInfo
		 * @param obj
		 * 
		 */		
		public function setOtherInfo(obj:Object):void{
			_objOtherInfo=obj;
		}
		/**
		 * 追加额外附加信息的属性
		 * 
		 */		
		public function appendOtherInfo(obj:Object):void{
			Fun.appendObj(obj,_objOtherInfo);
		}
		/**
		 * 获得额外附加信息  与 getOtherInfo()功能相同 
		 * @return 
		 * 
		 */		
		public function get objOtherInfo():Object{
			return _objOtherInfo;
		}
		/**
		 * 获得额外附加信息
		 * @return 
		 * 
		 */		
		public function getOtherInfo():Object{
			return _objOtherInfo;
		}
		public function start():void{
			loader.load(this.url);
		}
		//
		//如果错误，重新提交
		private var funLoadErrorToReUp_i:Number=0;
		private var funLoadErrorToReUp_count:Number=5;
		private function funLoadErrorToReUp():void{
			funLoadErrorToReUp_i++;
			if(funLoadErrorToReUp_i>funLoadErrorToReUp_count) return;
			loader.load(this.url);
		}
		private function addEventListener(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			//dispatcher.addEventListener(Event.OPEN, openHandler);
			//dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			//dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		private function removeEventListener(dispatcher:IEventDispatcher):void {
			dispatcher.removeEventListener(Event.COMPLETE, completeHandler);
			//dispatcher.removeEventListener(Event.OPEN, openHandler);
			//dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
			//dispatcher.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		private function completeHandler(event:Event):void {
			//trace("completeHandler: " + loader.data);
			try{
				funLoadCompt(loader.data,this);
			}catch(e:ArgumentError){
				funLoadCompt(loader.data);
			}
			removeEventListener(loader)
		}
		/*private function openHandler(event:Event):void {
		//trace("openHandler: " + event);
		}
		
		private function progressHandler(event:ProgressEvent):void {
		//trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
		}
		private function httpStatusHandler(event:HTTPStatusEvent):void {
		//trace("httpStatusHandler: " + event);
		}*/
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			//trace("securityErrorHandler: " + event);
			funLoadError();
			removeEventListener(loader)
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
			//trace("ioErrorHandler: " + event);
			funLoadError();
			removeEventListener(loader)
		}
		
	}
}